package cloud.xlh.util;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;

import java.util.Vector;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : xlh_osms
 * @ Author: XuLeHuang
 * @ Date: 2023/8/5 17:02
 * @ Description:
 */
public class SFTPUtil {
    //ip地址
    private String host;
    //登录账号
    private String username;
    //登录密码
    private String password;
    //默认端口号
    private int port;
    //默认过期时间
    private int timeout;

    public SFTPUtil(String host , String username ,String password ,int port , int timeout) {
        this.host = host;
        this.username = username;
        this.password = password;
        this.port = port;
        this.timeout = timeout;
    }

    public void put(String src , String dst) {
        SFTPChannel sftpChannel = new SFTPChannel(host, username, password , port , timeout);
        ChannelSftp channelSftp = null;
        try {
            channelSftp = sftpChannel.getChannel(sftpChannel);
        } catch (JSchException e) {
            e.printStackTrace();
        }
        System.out.println("创建链接");
        try {
            channelSftp.put(src, dst, ChannelSftp.OVERWRITE);
        } catch (SftpException e) {
            e.printStackTrace();
        }
        System.out.println("上传文件成功");
        //展示上传文件目录下的所有文件
        Vector vector = null;
        try {
            vector = channelSftp.ls(dst);
        } catch (SftpException e) {
            e.printStackTrace();
        }
        System.out.println(vector.toString());
        //关闭连接
        channelSftp.quit();
        sftpChannel.closeChannel();
    }

    public void remove(String dst) {
        SFTPChannel sftpChannel = new SFTPChannel(host, username, password , port , timeout);
        ChannelSftp channelSftp = null;
        try {
            channelSftp = sftpChannel.getChannel(sftpChannel);
        } catch (JSchException e) {
            e.printStackTrace();
        }
        System.out.println("创建链接");
        try {
            channelSftp.rm(dst);
        } catch (SftpException e) {
            e.printStackTrace();
        }
        System.out.println("上传文件成功");
        //展示上传文件目录下的所有文件
        Vector vector = null;
        try {
            vector = channelSftp.ls(dst);
        } catch (SftpException e) {
            e.printStackTrace();
        }
        System.out.println(vector.toString());
        //关闭连接
        channelSftp.quit();
        sftpChannel.closeChannel();
    }

    public void down(String src , String dst) {
        SFTPChannel sftpChannel = new SFTPChannel(host, username, password , port , timeout);
        ChannelSftp channelSftp = null;
        try {
            channelSftp = sftpChannel.getChannel(sftpChannel);
        } catch (JSchException e) {
            e.printStackTrace();
        }
        System.out.println("创建链接");
        try {
            channelSftp.get(src , dst);
        } catch (SftpException e) {
            e.printStackTrace();
        }
        System.out.println("下载文件成功");
        //展示上传文件目录下的所有文件
        Vector vector = null;
        try {
            vector = channelSftp.ls(dst);
        } catch (SftpException e) {
            e.printStackTrace();
        }
        System.out.println(vector.toString());
        //关闭连接
        channelSftp.quit();
        sftpChannel.closeChannel();
    }
}
